﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhDataAcess.orderDAL
{
   public class mrpMasterBLL
    {
       private mrpMasterTableAdapter _mrpMasterAdapter = null;
       private mrpMasterTableAdapter mrpMasterAdapter
        {
            get
            {
                if (_mrpMasterAdapter == null) _mrpMasterAdapter = new mrpMasterTableAdapter();
                return _mrpMasterAdapter;

            }
        }
        /// <summary>
       /// 查询生产用料主表
        /// </summary>
        /// <returns></returns>

       public orderDataSet.mrpMasterDataTable getmrpMasterAll()
        {
            try
            {
                return mrpMasterAdapter.GetDatamrpMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据漂染色单号查询生产用料主表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public orderDataSet.mrpMasterDataTable getmrpMasterAll(string orderDetailNo)
        {
            try
            {
                return mrpMasterAdapter.GetDatamrpMasterByorderDetailNo(orderDetailNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }

            /// <summary>
            /// 插入生产用料主表
            /// </summary>
            /// <param name="row"></param>
            /// <returns></returns>

        }
       public int mrpMasterInsert(mrpMasterModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;

            try
            {
                tn = conn.BeginTransaction();
                mrpMasterDAL mrpMaster = new mrpMasterDAL(tn, conn);
                mrpMaster.mrpMasterInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个客户编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }

            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 更新生产用料主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int mrpMasterUpdate(mrpMasterModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                mrpMasterDAL mrpMaster = new mrpMasterDAL(tn, conn);
                mrpMaster.mrpMasterUpdate (row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 删除一行生产用料主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int mrpMasterDel(string recordid)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                mrpMasterDAL mrpMaster = new mrpMasterDAL(tn, conn);
                mrpMaster.mrpMasterDel(recordid);
                tn.Commit();
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
